import os

from langchain_core.output_parsers import CommaSeparatedListOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

# 走代理，
os.environ['http_proxy'] = "127.0.0.1:7897"
os.environ['https_proxy'] = "127.0.0.1:7897"

os.environ['LANGCHAIN_TRACING_V2'] = "true"
#os.environ['LANGCHAIN_ENDPOINT'] = "https://api.smith.langchain.com"
os.environ['LANGCHAIN_API_KEY'] = "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"
#os.environ['LANGCHAIN_PROJECT'] = "pr-stupendous-culture-88"
# 设置 OpenAI API 密钥
os.environ['OPENAI_API_KEY'] = "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"
model = ChatOpenAI(model="gpt-3.5-turbo")
#out = model.invoke("列出成都的3个著名景点。")
#print(out.content)



prompt = ChatPromptTemplate.from_messages([
    ("system", "{parser_instructions}"),
    ("human", "请问{cityName}为什么是咸的？")
])
output_parser = CommaSeparatedListOutputParser()
parser_instructions = output_parser.get_format_instructions()
# 查看解析器的指令内容
print(f"内部的输出解释器：{parser_instructions}")

final_prompt = prompt.invoke({"cityName": "海水", "parser_instructions": parser_instructions})

response = model.invoke(final_prompt)
print(f"返回的原始内容：{response.content}")

ret = output_parser.invoke(response)
print(f"最终结果{ret}")
